{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2024 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "-I6OsLVasICN",
      "metadata": {
        "id": "-I6OsLVasICN"
      },
      "source": [
        "# PaLM API: Text Quickstart with Node.js"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lEXQ3OwKIa-O"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://ai.google.dev/palm_docs/text_node_quickstart\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />View on ai.google.dev</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/palm_docs/text_node_quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/google/generative-ai-docs/blob/main/site/en/palm_docs/text_node_quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ojFlwNm_sLb0",
      "metadata": {
        "id": "ojFlwNm_sLb0"
      },
      "source": [
        "## Obtain an API Key\n",
        "\n",
        "To get started, you'll need to [get an API key](setup).\n",
        "Set it as an environment variable:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "tjxQ-b-wsYP_",
      "metadata": {
        "id": "tjxQ-b-wsYP_"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "os.environ[\"API_KEY\"] = \"<YOUR API KEY>\""
      ]
    },
    {
      "cell_type": "markdown",
      "id": "FVV5j6OUtaCO",
      "metadata": {
        "id": "FVV5j6OUtaCO"
      },
      "source": [
        "Note: The use of `os.environ` above is only because this is a python based notebook. Working from the command line you would `export API_KEY=<YOUR API KEY>`."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "01566293",
      "metadata": {
        "id": "01566293"
      },
      "source": [
        "## Installing the API Client\n",
        "\n",
        "In a new directory, initialize a Node.js project using npm and install the\n",
        "`google-auth` library:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "01be273d",
      "metadata": {
        "id": "01be273d"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Wrote to /content/package.json:\n",
            "\n",
            "{\n",
            "  \"name\": \"content\",\n",
            "  \"version\": \"1.0.0\",\n",
            "  \"main\": \"index.js\",\n",
            "  \"scripts\": {\n",
            "    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n",
            "  },\n",
            "  \"keywords\": [],\n",
            "  \"author\": \"\",\n",
            "  \"license\": \"ISC\",\n",
            "  \"dependencies\": {\n",
            "    \"@google-ai/generativelanguage\": \"^1.0.1\",\n",
            "    \"google-auth-library\": \"^9.0.0\"\n",
            "  },\n",
            "  \"devDependencies\": {},\n",
            "  \"description\": \"\"\n",
            "}\n",
            "\n",
            "\n",
            "+ google-auth-library@9.0.0\n",
            "updated 1 package and audited 74 packages in 1.105s\n",
            "\n",
            "3 packages are looking for funding\n",
            "  run `npm fund` for details\n",
            "\n",
            "found 0 vulnerabilities\n",
            "\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "npm WARN content@1.0.0 No description\n",
            "npm WARN content@1.0.0 No repository field.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "npm init -y\n",
        "\n",
        "npm install google-auth-library"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a959bfe9",
      "metadata": {
        "id": "a959bfe9"
      },
      "source": [
        "Next, you'll need to install the Generative Language client library:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "23759631",
      "metadata": {
        "id": "23759631"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "+ @google-ai/generativelanguage@1.0.1\n",
            "updated 1 package and audited 74 packages in 2.126s\n",
            "\n",
            "3 packages are looking for funding\n",
            "  run `npm fund` for details\n",
            "\n",
            "found 0 vulnerabilities\n",
            "\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "npm WARN content@1.0.0 No description\n",
            "npm WARN content@1.0.0 No repository field.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "npm install @google-ai/generativelanguage"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7dbc42f9",
      "metadata": {
        "id": "7dbc42f9"
      },
      "source": [
        "## Generating Messages\n",
        "\n",
        "Create a new file `index.js` and add the following code, supplying your API key\n",
        "through the `API_KEY` environment variable:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "35f7a487",
      "metadata": {
        "id": "35f7a487"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Overwriting index.js\n"
          ]
        }
      ],
      "source": [
        "%%writefile index.js\n",
        "const { TextServiceClient } =\n",
        "  require(\"@google-ai/generativelanguage\").v1beta2;\n",
        "\n",
        "const { GoogleAuth } = require(\"google-auth-library\");\n",
        "\n",
        "const MODEL_NAME = \"models/text-bison-001\";\n",
        "const API_KEY = process.env.API_KEY;\n",
        "\n",
        "const client = new TextServiceClient({\n",
        "  authClient: new GoogleAuth().fromAPIKey(API_KEY),\n",
        "});\n",
        "\n",
        "const prompt = \"Repeat after me: one, two,\";\n",
        "\n",
        "client\n",
        "  .generateText({\n",
        "    model: MODEL_NAME,\n",
        "    prompt: {\n",
        "      text: prompt,\n",
        "    },\n",
        "  })\n",
        "  .then((result) => {\n",
        "    console.log(JSON.stringify(result, null, 2));\n",
        "  });"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b3d6aa7f",
      "metadata": {
        "id": "b3d6aa7f"
      },
      "source": [
        "Then run the script:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "9i7g4DIjr4Sk",
      "metadata": {
        "id": "9i7g4DIjr4Sk"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[\n",
            "  {\n",
            "    \"candidates\": [\n",
            "      {\n",
            "        \"safetyRatings\": [\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_DEROGATORY\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          },\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_TOXICITY\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          },\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_VIOLENCE\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          },\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_SEXUAL\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          },\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_MEDICAL\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          },\n",
            "          {\n",
            "            \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
            "            \"probability\": \"NEGLIGIBLE\"\n",
            "          }\n",
            "        ],\n",
            "        \"output\": \"One, two, three, four.\"\n",
            "      }\n",
            "    ],\n",
            "    \"filters\": [],\n",
            "    \"safetyFeedback\": []\n",
            "  },\n",
            "  null,\n",
            "  null\n",
            "]\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "\n",
        "node index.js"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "text_node_quickstart.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
